package com.zhuyi.broadcast.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuyi.broadcast.mapper.DeviceMapper;
import com.zhuyi.broadcast.mapper.DeviceScheduleRelationMapper;
import com.zhuyi.broadcast.mapper.ScheduleMapper;
import com.zhuyi.broadcast.mapper.ScheduleProgramRelationMapper;
import com.zhuyi.broadcast.pojo.domain.*;
import com.zhuyi.broadcast.service.AuditService;
import com.zhuyi.broadcast.service.DeviceService;
import com.zhuyi.common.constant.BroadcastConstants;
import com.zhuyi.common.utils.SecurityUtils;
import com.zhuyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {

    @Autowired
    private DeviceScheduleRelationMapper deviceScheduleRelationMapper;

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Autowired
    private AuditService auditService;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询设备列表
     * @param searchKey
     * @return
     */
    public List<Device> list(String searchKey) {
        LambdaQueryWrapper<Device> wrapper = Wrappers.lambdaQuery();
        wrapper.orderBy(true, false, Device::getCreateTime);
        if (ObjectUtils.isNotEmpty(searchKey)) {
            wrapper.like(Device::getName, searchKey);
        }
        return baseMapper.selectList(wrapper);
    }

    /**
     * 添加轮播计划
     * @param deviceId
     * @param scheduleId
     * @return
     */
    public boolean addScheduleToDevice(Long deviceId, Long scheduleId) {
        DeviceScheduleRelation deviceScheduleRelation = new DeviceScheduleRelation();
        deviceScheduleRelation.setScheduleId(scheduleId);
        deviceScheduleRelation.setDeviceId(deviceId);

        Schedule schedule = scheduleMapper.selectById(scheduleId);
        // 判断是否是超级管理员，是的话无需审核，不是的话需要审核
        if (sysUserRoleMapper.getRoleIdByUserId(SecurityUtils.getLoginUser().getUserId()) == 1) {
            schedule.setState(BroadcastConstants.RESOURCE_STATE_APPROVED);
        } else {
            schedule.setState(BroadcastConstants.RESOURCE_STATE_PENDING);
        }

        auditService.createAndSubmitAuditTask(schedule,"轮播计划");

        return deviceScheduleRelationMapper.insert(deviceScheduleRelation) > 0;
    }

    /**
     * 删除轮播计划
     * @param deviceId
     * @param scheduleId
     * @return
     */
    public boolean deleteScheduleFromDevice(Long deviceId, Long scheduleId) {
        deviceScheduleRelationMapper.delete(
                new LambdaQueryWrapper<DeviceScheduleRelation>()
                        .eq(DeviceScheduleRelation::getDeviceId,deviceId)
                        .eq(DeviceScheduleRelation::getScheduleId,scheduleId)
        );
        return true;
    }
}
